package com.manager.car.dao;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class BaseDao<T> extends HibernateDaoSupport{

	private SessionFactory factory;

	public SessionFactory getFactory() {
		return factory;
	}

	public void setFactory(SessionFactory factory) {
		this.factory = factory;
		this.setSessionFactory(factory);
	}
	
	public List<T> queryByHql(String hql,Object...args){
		List<T> list = (List<T>) this.getHibernateTemplate().find(hql, args);
		return list;
	}
	
	public T queryById(Integer id) {
		Class<T> c = (Class<T>) ((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0];
		return this.getHibernateTemplate().get(c, id);
	}
	
	public boolean save(T t) {
		this.getHibernateTemplate().save(t);
		return true;
	}
	public boolean update(T t) {
		this.getHibernateTemplate().update(t);
		return true;
	}
//	public void delete(Integer id) {
//		T t = this.queryById(id);
//		if(t != null)
//			this.getHibernateTemplate().delete(t);
//	}
	public boolean delete(Integer id) {
		T t = this.queryById(id);
		boolean bool = false;
		if(t != null) {
			this.getHibernateTemplate().delete(t);
			 bool = true;
		}else {
			 bool = false;
		}
		return bool;
	}
	
}
